package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.Complaints;
import cn.qf.shop.pojo.qo.ComplaintsQO;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.ComplaintsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.List;

@EnableSwagger2
@RequestMapping("/comp")
@Api(tags = "投诉管理业务接口")
@RestController
@RestControllerAdvice
public class ComplaintsController extends  BaseController {
    private Logger logger = LoggerFactory.getLogger(ComplaintsController.class);
            @Resource
    ComplaintsService complaintsService;
    /**
     * 查询投诉服务的所有表格
     */
    @ResponseBody
    @ApiOperation("投诉管理数据展示")
    @PostMapping(value = "/showComp", produces = {"application/json;charset=UTF-8"})
    public RespResult showComp(){
        List<Complaints> complaints = complaintsService.showComp();
        RespResult respResult =null;
        if (complaints == null) {
            respResult =super.returnRespWithFailed("找不到投诉表单");
        }
        respResult =super.returnRespWithSuccess(complaints);
        return  respResult;
    }
    /**
     * 增加一条字段
     *
     * @param complaintsQO
     * @return
     */
    @ResponseBody
    @ApiOperation("增加投诉管理数据")
    @PostMapping(value = "/regComp", produces = {"application/json;charset=UTF-8"})
    public RespResult regComp(@RequestBody ComplaintsQO complaintsQO) {
        Complaints complaints = new Complaints();
        BeanUtils.copyProperties(complaintsQO,complaints);
        RespResult respResult = new RespResult();
        int i = complaintsService.addComp(complaintsQO);
        if (i == 1) {
            respResult = showComp();
        } else {
            respResult = returnRespWithFailed("添加失败");
        }
        return respResult;
    }

    /**
     * 点击编辑
     *
     * @param complaintsQO
     * @return
     */
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/alterComp", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult alterComp(@RequestBody ComplaintsQO complaintsQO) {
        Complaints complaints = new Complaints();
        BeanUtils.copyProperties(complaintsQO,complaints);
        RespResult respResult = new RespResult();
        int i = complaintsService.updateComp(complaintsQO);
        if (i == 1) {
            respResult = showComp();
        } else {
            respResult = returnRespWithFailed("修改失败");
        }
        return respResult;
    }

    /**
     * 点击删除一条记录
     *
     * @param compId
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteCompId", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteCompId(int compId) {

        RespResult respResult = new RespResult();
        int i = complaintsService.deleteComp(compId);
        if (i == 1) {
            respResult =showComp();
        } else {
            respResult = returnRespWithFailed("删除失败");
        }
        return respResult;
    }

    /**
     * 点击图标进行全部删除
     *
     * @param compIds
     * @return
     */
    @ResponseBody
    @ApiOperation("批量删除")
    @RequestMapping(value = "/deleteAllComp", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteAllComp(@RequestParam(value = "compIds[]") List<Integer> compIds) {
        int i = complaintsService.deleteAllComp(compIds);
        RespResult respResult = null;
        if (i == -1) {
            respResult = super.returnRespWithFailed("删除失败");
        } else {
            respResult = showComp();
        }
        return respResult;
    }

    @ResponseBody
    @ApiOperation("用户模糊查询")
    @RequestMapping(value = "/selectAllComp", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult selectAllComp(@RequestParam(required = false) String keyword, @RequestParam(required = false) String createTime, @RequestParam(required = false) String updateTime, @RequestParam(defaultValue = "1") int limit, @RequestParam(defaultValue = "10") int offset) {
        RespResult respResult = null;
        List<ComplaintsQO> complaintsQOS = complaintsService.COMPLAINTSList(keyword, createTime, updateTime, limit, offset);
        if (complaintsQOS.size() == 0) {
            respResult = returnRespWithFailed("找不到实体类");
        } else {
            RespResult respResult1 = returnRespWithSuccess(complaintsQOS);
            return respResult1;
        }
        return respResult;
    }
}
